// 定义转换规则接口
export interface ProSchema<T> {
    dataIndex: string; // 目标对象中的键名
    sourceKey: string; // 源属性或取值函数
    title?: string; // 可选标题
    render?: React.ReactNode;
}

export const omitUndefinedAndEmptyArr = <T extends Record<string, any>>(
    obj: T,
): T => {
    const newObj = {} as Record<string, any> as Record<string, any>;
    Object.keys(obj || {}).forEach((key) => {
        if (Array.isArray(obj[key]) && obj[key]?.length === 0) {
            return;
        }
        if (obj[key] === undefined) {
            return;
        }
        newObj[key] = obj[key];
    });
    return newObj as T;
};


// 转换单个数据对象
export function convertData<T>(data: T, columnProps: ProSchema<T>[]): Record<string, any> {
    const result: Record<string, any> = {};
    columnProps.forEach(rule => {
        // 获取原始值
        let value = (data as any)[rule.sourceKey];

        const columnKey = rule.dataIndex;

        // 赋值到目标属性
        result[columnKey] = value;
    })

    return result;
}

export function convertDataList<T>(columns: T[], columnProps: ProSchema<T>[]): Record<string, any>[] {
    return columns?.map(data => convertData(data, columnProps));
}